package com.ruoyi.project.module.shao;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.HeAnalysis;
import com.ruoyi.project.domain.HeEventFlow;
import com.ruoyi.project.domain.HeEventReport;
import com.ruoyi.project.domain.HeReportEvent;
import com.ruoyi.project.service.IHeAnalysisService;
import com.ruoyi.project.service.IHeEventFlowService;
import com.ruoyi.project.service.IHeEventReportService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 分析报告
Controller
 * 
 * @author ruoyi
 * @date 2023-09-02
 */
@RestController
@RequestMapping("/shao/analysis")
public class ShaoAnalysisController extends BaseController
{
    @Autowired
    private IHeAnalysisService heAnalysisService;
    @Autowired
    private IHeEventFlowService heEventFlowService;
    @Autowired
    private IHeEventReportService heEventReportService;

    /**
     * 查询分析报告
列表
     */
//    @RequiresPermissions("project:analysis:list")
    @GetMapping("/list")
    public TableDataInfo list(HeAnalysis heAnalysis)
    {

        List<HeAnalysis> list = heAnalysisService.selectHeAnalysisList(heAnalysis);
        if(list.size()==0){
            list=new ArrayList<>();

        }
        return getDataTable(list);
    }

    /**
     * 导出分析报告
列表
     */
//    @RequiresPermissions("project:analysis:export")
    @Log(title = "分析报告 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HeAnalysis heAnalysis)
    {
        List<HeAnalysis> list = heAnalysisService.selectHeAnalysisList(heAnalysis);
        ExcelUtil<HeAnalysis> util = new ExcelUtil<HeAnalysis>(HeAnalysis.class);
        util.exportExcel(response, list, "分析报告数据");
    }

    /**
     * 获取分析报告
详细信息
     */
//    @RequiresPermissions("project:analysis:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        HeAnalysis heAnalysis=new HeAnalysis();
        heAnalysis.setReportEventId(id);
        List<HeAnalysis> list = heAnalysisService.selectHeAnalysisList(heAnalysis);
        if(list.size()==0){
            return new AjaxResult(200,"请填分析报告");
        }
        return success(heAnalysisService.selectHeAnalysisById(list.get(0).getId()));
    }

    /**
     * 新增分析报告
     */
//    @RequiresPermissions("project:analysis:add")
    @Log(title = "分析报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HeAnalysis heAnalysis)
    {
        //获取登录人的userid
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        heAnalysis.setUserId(sysUser.getUserId());
        //======以下是对报告表report的操作======
        //将协助人部门信息注入到report表中的对应字段中,首先先通过前端传过来的this.analysisform.reportEventId=this.$route.query.id;来拿到reportEventId
        //首先新建个heEventReport对象
        HeEventReport heEventReport= new HeEventReport();
        //再将拿到的reportEventId调用report表中的selectbyid的方法进而拿到那条数据
        heEventReport=heEventReportService.selectHeEventReportById(heAnalysis.getReportEventId());
        //在前端将协助部门一和二存入到note2和note3中这里拿到了它的值将其set到report表对应的字段中
        heEventReport.setSupportDepartmentOne(heAnalysis.getNote2());
        heEventReport.setSupportDepartmentTwo(heAnalysis.getNote3());
        //进行修改
        heEventReportService.updateHeEventReport(heEventReport);
        //======以下是对流转表的操作======
        //获取当前时间
        heAnalysis.setCreationTime(DateUtils.getNowDate());
        String pin=heAnalysis.getNote1();
        pin =sysUser.getNickName()+"填写了"+pin;
        //新建个流转表对象
        HeEventFlow heEventFlow=new HeEventFlow();
        //将分析表的reporteventid注入到flow表的reporteventid中
        heEventFlow.setReportEventId(heAnalysis.getReportEventId());
        heEventFlow.setCreationTime(DateUtils.getNowDate());
        heEventFlow.setProcessStreamContent(pin);
        //新增到流转表
        heEventFlowService.insertHeEventFlow(heEventFlow);
        //将其置空防止造成脏数据(这只是一个变量用来接值的)
        heAnalysis.setNote1(null);
        heAnalysis.setNote2(null);
        heAnalysis.setNote3(null);
        //最后新增分析报告表
        return toAjax(heAnalysisService.insertHeAnalysis(heAnalysis));
    }

    /**
     * 编辑分析报告
     */
//    @RequiresPermissions("project:analysis:edit")
    @Log(title = "分析报告 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HeAnalysis heAnalysis)
    {
        //获取登录人的userid
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        String pin=heAnalysis.getNote1();
        pin =sysUser.getNickName()+"编辑了"+pin;
        heAnalysis.setNote1(null);
        //新建个流转表对象
        HeEventFlow heEventFlow=new HeEventFlow();
        //将分析表的reporteventid注入到flow表的reporteventid中
        heEventFlow.setReportEventId(heAnalysis.getReportEventId());
        heEventFlow.setCreationTime(DateUtils.getNowDate());
        heEventFlow.setProcessStreamContent(pin);
        heEventFlowService.insertHeEventFlow(heEventFlow);
        return toAjax(heAnalysisService.updateHeAnalysis(heAnalysis));
    }

    /**
     * 删除分析报告

     */
//    @RequiresPermissions("project:analysis:remove")
    @Log(title = "分析报告 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(heAnalysisService.deleteHeAnalysisByIds(ids));
    }
}
